/**
* Tools services
*/
app.factory("toolsServices", function($http) {	
	var messages = [];
	var parameters = undefined;
	var currentUser = undefined;
	return {
		setParameters: function(json){
			parameters = json;
		},
		getParameters: function(){
			return parameters;
		},
		setCurrentUser: function(data){
			console.info("setCurrentUser - called with data : " + data); 
			if(typeof(Storage) != undefined) 
			{
				localStorage.setItem("CurrentUser", JSON.stringify(data));
			} 
			else
			{
				$cookieStore.put("CurrentUser", JSON.stringify(data));
			}
			currentUser = data;
		},
		getCurrentUser: function(){
			console.info("getCurrentUser - called"); 
			if(typeof(Storage) != undefined) 
			{
				currentUser = JSON.parse(localStorage.getItem("CurrentUser"));
			} 
			else 
			{
				currentUser = JSON.parse($cookieStore.get('CurrentUser'));
			}
			return currentUser;
		},
		removeCurrentUser: function(){
			console.info("removeCurrentUser - called"); 
			if(typeof(Storage) != undefined) 
			{
				currentUser = localStorage.removeItem("CurrentUser");
			} 
			else 
			{
				currentUser = $cookieStore.remove('CurrentUser');
			}
			currentUser = undefined;
		},
		displaySuccess: function(message) {
			console.info("toolsServices.displaySuccess called - " + message);
			var newId = "message_" + messages.length;
			var template = "<div id='" + newId + "' class='alert alert-success alert-dismissible' role='alert' >";
			template += "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>";
			template += message;
			template += "</div>";
			messages.push(template);
			$('#messages').append(template);
			$('#messages').fadeIn();
			setTimeout( function () { 
				$('#messages').fadeOut( "slow", function() {
					$('#' + newId).remove();
				});
			}, 3000);
		},
		displayInfo: function(message) {
			console.info("toolsServices.displayInfo called - " + message);
			var newId = "message_" + messages.length;
			var template = "<div id='" + newId + "' class='alert alert-info alert-dismissible' role='alert' >";
			template += "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>";
			template += message;
			template += "</div>";
			messages.push(template);
			$('#messages').append(template);
			$('#messages').fadeIn();
			setTimeout( function () { 
				$('#messages').fadeOut( "slow", function() {
					$('#' + newId).remove();
				});
			}, 3000);
		},
		displayWarning: function(message) {
			console.info("toolsServices.displayWarning called - " + message);
			var newId = "message_" + messages.length;
			var template = "<div id='" + newId + "' class='alert alert-warning alert-dismissible' role='alert' >";
			template += "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>";
			template += message;
			template += "</div>";
			messages.push(template);
			$('#messages').append(template);
			$('#messages').fadeIn();
			setTimeout( function () { 
				$('#messages').fadeOut( "slow", function() {
					$('#' + newId).remove();
				});
			}, 3000);
		},
		displayError: function(message) {
			console.info("toolsServices.displayError called - " + message);
			var newId = "message_" + messages.length;
			var template = "<div id='" + newId + "' class='alert alert-danger alert-dismissible' role='alert' >";
			template += "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>";
			template += message;
			template += "</div>";
			messages.push(template);
			$('#messages').append(template);
			$('#messages').fadeIn();
			setTimeout( function () { 
				$('#messages').fadeOut( "slow", function() {
					$('#' + newId).remove();
				});
			}, 3000);
		}
	};
});
